/**
 * 
 */
package org.springside.examples.miniweb.web.customer;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.examples.miniweb.entity.customer.Customer;
import org.springside.examples.miniweb.entity.search.SearchForm;
import org.springside.examples.miniweb.service.customer.CustomerManager;
import org.springside.examples.miniweb.web.account.UserController;
import org.springside.examples.miniweb.web.report.ReportController;

/**
 * @author Bob
 * 
 * 
 */
@Controller
@RequestMapping(value = "/customer")
public class CustomerController extends ReportController<Customer> {

	private CustomerManager manager;

	/**
	 * @param manager
	 *            the manager to set
	 */
	@Autowired
	public void setManager(CustomerManager manager) {
		this.manager = manager;
	}

	@RequestMapping(value = { "list", "" })
	public String list(Model model) {
		SearchForm searchForm = new SearchForm();
		model.addAttribute("search", searchForm);
		List<Customer> list = manager.getAllCustomer();
		Page<Customer> pages = getPages(searchForm, list);
		model.addAttribute("pages", pages);
		model.addAttribute("customers", pages.getContent());
		return "customer/customerList";
	}

	@RequestMapping(value = "create")
	public String createForm(Model model, HttpServletRequest request) {
		model.addAttribute("add", true);
		model.addAttribute("customer", new Customer());
		return "customer/customerForm";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "search")
	public String search(@ModelAttribute("search") SearchForm searchForm,
			Model model, HttpServletRequest request) {
		model.addAttribute("search", searchForm);
		List<Customer> list = null;
		String customerName = searchForm.getCustomerName().trim();
		if (searchForm.getPageNumber().equalsIgnoreCase("research")) {
			if("".equals(customerName)){
				list = manager.getAllCustomer();
			}else{
				list = manager.findByCustomerNameLike(searchForm.getCustomerName());
			}
			searchForm.setPageNumber("0");
			request.getSession().setAttribute("customerList", list);
		} else {
			list = (List<Customer>) request.getSession().getAttribute(
					"customerList");
		}
		Page<Customer> pages = getPages(searchForm, list);
		model.addAttribute("customers", pages.getContent());
		model.addAttribute("pages", pages);
		return "customer/customerList";
	}

	@RequestMapping(value = "save/*")
	public String save(Customer customer, RedirectAttributes redirectAttributes) {
		manager.saveCustomer(customer);
		redirectAttributes.addFlashAttribute("message",
				"更新客户：" + customer.getName() + "成功");
		return "redirect:/customer/list";
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id,
			RedirectAttributes redirectAttributes) {
		boolean permitted = SecurityUtils.getSubject().isPermitted(
				"customer:edit");
		if (!permitted) {
			return "redirect:/logout";
		}
		manager.deleteCustomer(id);
		redirectAttributes.addFlashAttribute("message", "删除客户成功");
		return "redirect:/customer/list";
	}

	@RequestMapping(value = "update/{id}")
	public String update(@PathVariable("id") Long id, Model model,
			RedirectAttributes redirectAttributes) {
		boolean permitted = SecurityUtils.getSubject().isPermitted(
				"customer:edit");
		if (!permitted) {
			return "redirect:/logout";
		}
		model.addAttribute("customer", manager.getCustomerById(id));
		return "customer/customerForm";
	}

	@RequestMapping(value = "checkName")
	@ResponseBody
	public String checkName(@RequestParam("oldName") String oldName,
			@RequestParam("name") String name) {
		String decodeName = UserController.decodeURIComponent(name);
		if (oldName.equals(name)) {
			return "true";
		} else if (manager.findByCustomerName(decodeName) == null) {
			return "true";
		}
		return "false";
	}

}
